#include "net.h"

/* 客户端 */
int main()
{

	int fd;
	struct sockaddr_in soc;
	/* 创建套接字 */
	if( (fd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
	{
		perror("socket");
		exit(1);
	}

	/* 填充结构体 */
	bzero(&soc, sizeof(soc));
	soc.sin_family = AF_INET;	/* ipv4*/
	soc.sin_port = htons(PORT);  //端口号
	if (inet_pton (AF_INET, IP_ADDR4, (void *) &soc.sin_addr) != 1) {
		perror ("inet_pton");
		exit (1);
	}


	if (connect (fd, (struct sockaddr *) &soc, sizeof (soc)) < 0) {
		perror ("connect");
		exit (1);
	}

	printf ("Client staring...OK!\n");
	/*3. 读写数据 */
	char buf[BUFSIZ];
	int ret = -1;
	while (1) {
		bzero (buf, BUFSIZ);
		if (fgets (buf, BUFSIZ - 1, stdin) == NULL) {
			continue;
		}
		do {
			ret = write (fd, buf, strlen (buf));
		} while (ret < 0 && EINTR == errno);

		if (!strncasecmp (buf, QUIT, strlen (QUIT))) {	//用户输入了quit字符
			printf ("Client is exiting!\n");
			break;
		}
	}

	/*4.关闭套接字 */
	close (fd);


}

